package com.config.dto;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiModelProperty;
import java.util.List;
import java.util.Objects;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

/**
 * @author zhangquan
 */
@Data
@EqualsAndHashCode
public class PageResult<T> extends BaseResult {

    private final static PageResult<?> EMPTY = new PageResult<>();
    @ApiModelProperty(value = "分页对象")
    private PageData<T> data;



    private PageResult() {
        this.data = null;
    }

    private PageResult(String message, int code) {
        super(message,code);
    }
    private PageResult(ResultCode resultCode) {
        super(resultCode.getMsg(),resultCode.getCode());
    }

    private PageResult(List<T> list, int code) {
        super(null,code);
        PageData<T> data = new PageData<>(list);
        this.data = data;
    }
    private PageResult(IPage<T> page, int code) {
        super(null,code);
        PageData<T> data = new PageData<>(page);
        this.data = data;
    }

    /**
     * 功能描述: 创建一个空PageResult类
     *
     * @auther: Jason
     * @date: 2018/4/21
     */
    public static <T> PageResult<T> empty() {
        @SuppressWarnings("unchecked")
        PageResult<T> t = (PageResult<T>) EMPTY;
        return t;
    }

    /**
     * 功能描述: 生成一个成功状态PageResult类
     *
     * @auther: Jason
     * @param: List<T>
     * @return: PageResult<T>
     * @date: 2018/4/21
     */
    public static <T> PageResult<T> success(List<T> data) {
        return new PageResult<>(data, ResponseCode.SUCCESS.getCode());
    }
    public static <T> PageResult<T> success(IPage<T> page) {
        return new PageResult<>(page, ResponseCode.SUCCESS.getCode());
    }



    /**
     * 功能描述: 生成一个失败状态PageResult类
     *
     * @auther: Jason
     * @param: 错误信息
     * @return: PageResult<T>
     * @date: 2018/4/21
     */
    public static <T> PageResult<T> fail(String message) {
        return new PageResult<>(message, ResponseCode.FAIL.getCode());
    }

    /**
     * 功能描述: 设置页码信息
     *
     * @auther: Jason
     * @param: pageNum, pageSize
     * @return: PageResult<T>
     * @date: 2018/4/21
     */
    public <T> PageResult<T> page(int pageNo, int pageSize) {
        if (ResponseCode.SUCCESS.getCode() == super.getCode()) {
            data.setPageNo(pageNo);
            data.setPageSize(pageSize);
        }
        return (PageResult<T>) this;
    }
    public <T> PageResult<T> page(PageRequest pageRequest){
        return page(pageRequest.getPageNo(),pageRequest.getPageSize());
    }

    /**
     * 功能描述: 设置总数
     *
     * @auther: Jason
     * @param: total
     * @return: PageResult<T>
     * @date: 2018/4/21
     */
    public <T> PageResult<T> total(int total) {
        if (data != null) {
            data.setTotal(total);
        }
        return (PageResult<T>) this;
    }
    /**
     * 功能描述: 判断是否传入值是否为空,非空则返回值，为空则返回失败信息
     *
     * @auther: Jason
     * @param: 返回的错误信息
     * @return: PageResult<T>
     * @date: 2018/4/21
     */
    public <T> PageResult<T> orFail(String message) {
        if (null != data.records) {
            return (PageResult<T>) this;
        } else {
            super.setCode(ResponseCode.FAIL.getCode());
            super.setMsg(message);
//            this.msg = message;
//            this.code = "0";
        }
        return (PageResult<T>) this;
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(data);
    }


    @Override
    public String toString() {
        return data != null
                ? String.format("result[%s]", data)
                : "result.empty";
    }

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class PageData<T> {
        /**
         * 接口数据
         */
        @ApiModelProperty(value = "结果集")
        private List<T> records;
        @ApiModelProperty(value = "当前页码",example = "1")
        private long pageNo;
        @ApiModelProperty(value = "总记录数",example = "100")
        private long total;
        @ApiModelProperty(value = "页容量",example = "20")
        private long pageSize;

        public  PageData(List<T> records){
            this.records = records;
        }
        public PageData(IPage<T> page) {
            this.total = page.getTotal();
            this.records = page.getRecords();
            this.pageNo = page.getCurrent();
            this.pageSize = page.getSize();
        }
    }
}